//
// Created by WYLH2 on 2024/8/20.
//

#include "PocketCubeCorner.h"

#include <utility>

PocketCubeCorner::PocketCubeCorner(std::vector<int> coord, std::vector<std::string> color)
    : coord(std::move(coord)), color(std::move(color)) {}

std::vector<int> PocketCubeCorner::rotate_vector_around_axis_optimized(std::vector<int> v, int axis, bool clockwise) {
    int x = v[0], y = v[1], z = v[2];

    auto mix = [&](int &a, int &b, bool clockwise) {
        int flag = a * b * (clockwise ? 1 : -1);
        if (flag == 1) {
            return std::make_pair(a, -b);
        } else {
            return std::make_pair(-a, b);
        }
    };

    if (axis == 0) { // x
        std::tie(z, y) = mix(z, y, clockwise);
    } else if (axis == 1) { // y
        std::tie(x, z) = mix(x, z, clockwise);
    } else if (axis == 2) { // z
        std::tie(y, x) = mix(y, x, clockwise);
    }

    return {x, y, z};
}

void PocketCubeCorner::rotate(int axis, bool clockwise) {
    // Rotate colors
    std::vector<int> color_exchange_index;
    for (int i = 0; i < 3; ++i) {
        if (i != axis) color_exchange_index.push_back(i);
    }

    std::swap(color[color_exchange_index[0]], color[color_exchange_index[1]]);

    // Rotate coords
    coord = rotate_vector_around_axis_optimized(coord, axis, clockwise);
}

std::vector<int> PocketCubeCorner::getCoord() const {
    return coord;
}

std::vector<std::string> PocketCubeCorner::getColor() const {
    return color;
}


